package cc.orange.mainView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
//import com.tendcloud.tenddata.TalkingDataSDK;
import com.xcheng.retrofit.Call;
import com.xcheng.retrofit.DefaultCallback;
import com.xcheng.retrofit.HttpError;
import com.xcheng.retrofit.RetrofitFactory;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import cc.orange.R;
import cc.orange.adapter.MsgConSystemAdapter;
import cc.orange.base.BaseActivity;
import cc.orange.databinding.ActivityConversationListBinding;
import cc.orange.entity.ChatHistoryEntity;
import cc.orange.entity.IsLoginsEntity;
import cc.orange.http.ApiService;
import cc.orange.utils.ZXToastUtil;
import demo.smart.access.xutlis.util.ZXAppUtil;

public class ConversatioSystemActivity extends BaseActivity implements View.OnClickListener{

    private ActivityConversationListBinding binding;
    private String uid;
    private MsgConSystemAdapter adapter;
    private List<ChatHistoryEntity.Data.ListEntity> list = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_conversation_list);
        uid = getIntent().getStringExtra("uid");
        binding.talkTab1.setText("系统消息");
        binding.talkTab2.setOnClickListener(this);
        initViews();
        findChat();
    }

    private void initViews() {
        binding.talkMsgNotice.setVisibility(View.GONE);
        binding.talkEdits2.setVisibility(View.GONE);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//        linearLayoutManager.setStackFromEnd(true);
        binding.talkMsgList.setLayoutManager(linearLayoutManager);
        adapter = new MsgConSystemAdapter(list);
        binding.talkMsgList.setAdapter(adapter);

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(ConversatioSystemActivity.this, AlbumPreviewActivity.class);
                ArrayList<String> listsf = new ArrayList<>();
                listsf.add("" + list.get(position).getPicUrl());
                intent.putStringArrayListExtra("p_media", listsf);
                intent.putExtra("albn_where",1);
                startActivity(intent);
            }
        });

    }


    @Override
    protected void onPause() {
        super.onPause();
        EventBus.getDefault().post(new IsLoginsEntity(true));
//        TalkingDataSDK.onPageEnd(this, getLocalClassName());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.talk_tab_2:
                finish();
                break;
        }
    }


    // 查找聊天记录
    private void findChat(){
        RetrofitFactory.create(ApiService.class)
                .findChat(getTokens(),uid, ZXAppUtil.getPackageName())
                .enqueue(new DefaultCallback<ChatHistoryEntity>() {
                    @Override
                    public void onStart(Call<ChatHistoryEntity> call) {
                        showLoading_base(ConversatioSystemActivity.this);
                    }

                    @Override
                    public void onError(Call<ChatHistoryEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<ChatHistoryEntity> call, ChatHistoryEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                            list.addAll(baseEntity.getData().getList());
//                            Collections.reverse(list);
                            adapter.setNamePhoto(baseEntity.getData().getName(),baseEntity.getData().getPhoto(),uid);
                            adapter.notifyDataSetChanged();
                            if(list.size() > 1) {
                                binding.talkMsgList.smoothScrollToPosition(adapter.getItemCount() - 1);
                            }
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
//        TalkingDataSDK.onPageBegin(this, getLocalClassName());
    }

}